package com.sh.data.engine.web.webapi.controller.api;

import com.sh.data.engine.application.model.dto.api.DataApiBaseResponse;
import com.sh.data.engine.application.model.dto.api.DataApiResponse;
import com.sh.data.engine.application.model.dto.api.TagResponse;
import com.sh.data.engine.application.model.dto.common.DirectoryResponse;
import com.sh.data.engine.application.model.request.api.DataApiMyApiByTagRequest;
import com.sh.data.engine.application.model.request.api.DataApiMyApiRequest;
import com.sh.data.engine.application.model.request.api.DataApiTagDeleteRequest;
import com.sh.data.engine.application.model.request.api.DataApiTagSaveRequest;
import com.sh.data.engine.application.model.request.common.UpsertDirectoryRequest;
import com.sh.data.engine.application.service.api.DataApiMyApiApplicationService;
import com.sh.data.engine.common.annotation.fuzzyQuery.FuzzyMethod;
import com.sh.data.engine.common.enumDefinition.ProjectStatisticTypeEnum;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.ProjectStatisticExecution;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.annotation.userlog.EngineOperation;
import com.sh.data.engine.domain.base.model.PageResult;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author : fuchen
 * @date : 2022/1/21 11:04
 */
@Validated
@RestController
@Tag(name = "数据服务-我的API", description = "数据服务-我的API")
@RequestMapping("/myApi")
public class DataApiMyApiController {

    @Autowired
    private DataApiMyApiApplicationService dataApiMyApiApplicationService;

    @Operation(summary = "获取目录", description = "获取目录")
    @FuzzyMethod(fuzzyParamIndex = 0)
    @GetMapping("/getApiGroup")
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 0)
    public RestResponseEntity<List<DirectoryResponse>> getDirList(
        @Parameter(description = "目录搜索", required = false) @RequestParam(value = "search", required = false)
        String search) {
        var result = dataApiMyApiApplicationService.getDirList(search);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "获取标签目录", description = "获取标签目录")
    @FuzzyMethod(fuzzyParamIndex = 0)
    @GetMapping("/getApiTag")
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 0)
    public RestResponseEntity<List<DirectoryResponse>> getTagList(
        @Parameter(description = "tag搜索", required = false) @RequestParam(value = "search", required = false)
        String search) {
        var result = dataApiMyApiApplicationService.getTagList(search);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "创建,更新标签", description = "创建,更新标签")
    @PostMapping("/saveDataApiTag")
    @LogExecution(operation = EngineOperation.ADD_UPDATE, business = Business.数据服务标签)
//    @Log(
//        business = Business.数据服务标签,
//        operation = Operation.ADD_UPDATE)
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 1)
    public RestResponseEntity<Boolean> createApiTag(@RequestBody UpsertDirectoryRequest request) {
        var result = dataApiMyApiApplicationService.createApiTag(request);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "删除标签", description = "删除标签")
    @PostMapping("/deleteApiTag")
    @LogExecution(operation = EngineOperation.DEL, business = Business.数据服务标签)
//    @Log(
//        business = Business.数据服务标签,
//        operation = Operation.DEL)
    public RestResponseEntity<Boolean> deleteApiTag(
        @RequestBody @Valid DataApiTagDeleteRequest dataApiTagDeleteRequest) {
        var result = dataApiMyApiApplicationService.deleteApiTag(dataApiTagDeleteRequest);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "获取当前API归属的标签", description = "获取当前API归属的标签")
    @GetMapping("/getApiTagById")
    public RestResponseEntity<List<TagResponse>> getApiTagById(
        @Parameter(description = "apiId", required = true) @RequestParam(value = "apiId", required = true)
        Long apiId) {
        var result = dataApiMyApiApplicationService.getApiTagById(apiId);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "编辑当前API归属的标签", description = "编辑当前API归属的标签")
    @PostMapping("/saveApiTagById")
    @LogExecution(operation = EngineOperation.UPDATE, business = Business.数据服务标签)
//    @Log(
//        business = Business.数据服务标签,
//        operation = Operation.UPDATE)
    public RestResponseEntity<Boolean> saveApiTagById(
        @RequestBody @Valid DataApiTagSaveRequest request) {
        var result = dataApiMyApiApplicationService.saveApiTagById(request);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "POST_通过标签获取我的api列表", description = "和getMyApiList接口类似,移除了groupId参数,添加了tagId参数")
    @PostMapping("/getMyApiListByTag")
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 0)
    public RestResponseEntity<PageResult<DataApiBaseResponse>> getMyApiListByTag(
        @RequestBody @Valid DataApiMyApiByTagRequest dataApiMyApiRequest) {
        var result = dataApiMyApiApplicationService.getMyApiListByTag(dataApiMyApiRequest);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "新增、更新目录", description = "新增、更新目录")
    @PostMapping("/saveDataApiGroup")
    @LogExecution(operation = EngineOperation.ADD_UPDATE, business = Business.数据服务目录)
//    @Log(
//        business = Business.数据服务目录,
//        operation = Operation.ADD_UPDATE)
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 1)
    public RestResponseEntity<Boolean> upsetDir(@RequestBody UpsertDirectoryRequest request) {
        var result = dataApiMyApiApplicationService.upsetDir(request);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 1)
    @Operation(summary = "删除目录", description = "删除目录")
    @LogExecution(operation = EngineOperation.DEL, business = Business.数据服务目录)
//    @Log(
//        business = Business.数据服务目录,
//        operation = Operation.DEL)
    @GetMapping("/removeDataApiGroupById")
    public RestResponseEntity<Boolean> deleteDir(Long id) {
        var result = dataApiMyApiApplicationService.deleteDir(id);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "删除 api")
    @LogExecution(operation = EngineOperation.DEL, business = Business.数据服务)
//    @Log(
//        business = Business.数据服务,
//        operation = Operation.DEL)
    @PostMapping("/removeDataApiById")
    @ProjectStatisticExecution(type = ProjectStatisticTypeEnum.API)
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 1)
    public RestResponseEntity<Boolean> removeDataApiById(Long id) {
        var result = dataApiMyApiApplicationService.removeDataApiById(id);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "查看详情")
    @GetMapping("/getDataApiDetailById")
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 0)
    public RestResponseEntity<DataApiResponse> getDataApiDetailById(Long id) {
        var result = dataApiMyApiApplicationService.getDataApiDetailById(id);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/exportApi")
    @Operation(summary = "导出api，apiId参数不传时为批量导出")
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 0)
    public void exportApi(
        @Parameter(description = "以逗号割开", required = true) @RequestParam String apiIds,
        HttpServletResponse response) {
        dataApiMyApiApplicationService.exportApi(apiIds, response);
    }

    @Operation(summary = "api取消发布")
    @PostMapping("/unPublishApiById")
    @LogExecution(operation = EngineOperation.DEPUBLISH, business = Business.数据服务)
//    @Log(
//        business = Business.数据服务,
//        operation = Operation.DEPUBLISH)
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 1)
    public RestResponseEntity<Boolean> unPublishApiById(@RequestParam(name = "id") Long id) {
        var result = dataApiMyApiApplicationService.unPublishApiById(id);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "api批量取消发布")
    @PostMapping("/unPublishApiByIds")
    @LogExecution(operation = EngineOperation.DEPUBLISH, business = Business.数据服务)
//    @Log(
//        business = Business.数据服务,
//        operation = Operation.DEPUBLISH)
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 1)
    public RestResponseEntity<Boolean> unPublishApiByIds(String ids) {
        var result = dataApiMyApiApplicationService.unPublishApiByIds(ids);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "api发布")
    @PostMapping("/publishApiById")
    @LogExecution(operation = EngineOperation.PUBLISH, business = Business.数据服务)
//    @Log(
//        business = Business.数据服务,
//        operation = Operation.PUBLISH)
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 1)
    public RestResponseEntity<Boolean> publishApiById(@RequestParam(name = "id") Long id) {
        var result = dataApiMyApiApplicationService.publishApiById(id);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "api批量发布")
    @PostMapping("/publishApiByIds")
    @LogExecution(operation = EngineOperation.PUBLISH, business = Business.数据服务)
//    @Log(
//        business = Business.数据服务,
//        operation = Operation.PUBLISH)
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 1)
    public RestResponseEntity<Boolean> publishApiByIds(String ids) {
        var result = dataApiMyApiApplicationService.publishApiByIds(ids);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "我的api 列表")
    @GetMapping("/getMyApiList")
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 0)
    public RestResponseEntity<PageResult<DataApiBaseResponse>> getMyApiList(
        @Valid DataApiMyApiRequest dataApiMyApiRequest) {
        var result = dataApiMyApiApplicationService.getMyApiList(dataApiMyApiRequest);
        return RestResponseEntity.success(result);
    }
}
